/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl;

import cz.muni.fi.diplomka.datalayer.dao.AllianceDao;
import cz.muni.fi.diplomka.datalayer.dao.AllianceMemberDao;
import cz.muni.fi.diplomka.datalayer.daoimpl.AllianceDaoImpl;
import cz.muni.fi.diplomka.datalayer.daoimpl.AllianceMemberDaoImpl;
import cz.muni.fi.diplomka.datalayer.entity.AllianceEntity;
import cz.muni.fi.diplomka.datalayer.entity.AllianceMemberEntity;
import cz.muni.fi.diplomka.servicelayer.dto.Alliance;
import cz.muni.fi.diplomka.servicelayer.dto.AllianceMember;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.AlliancesManager;
import java.util.ArrayList;
import java.util.List;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;

/**
 *
 * @author Jakub Kutil
 */
public class AlliancesManagerImpl implements AlliancesManager {

    private AllianceDao allianceDao = new AllianceDaoImpl();
    private AllianceMemberDao allianceMemberDao = new AllianceMemberDaoImpl();
    private Mapper mapper = new DozerBeanMapper();

    @Override
    public void createAlliance(Alliance alliance) {
        if (alliance == null) {
            throw new IllegalArgumentException("Alliance cant be null");
        }
        allianceDao.addAlliance(mapper.map(alliance, AllianceEntity.class));
    }

    @Override
    public void editAlliance(Alliance alliance) {
        if (alliance == null) {
            throw new IllegalArgumentException("Alliance cant be null");
        }
        allianceDao.editAlliance(mapper.map(alliance, AllianceEntity.class));
    }

    @Override
    public void deleteAlliance(Long allianceId) {
        if (allianceId == null) {
            throw new IllegalArgumentException("Alliance ID cant be null");
        }
        allianceDao.deleteAlliance(allianceId);
    }

    @Override
    public List<AllianceMember> getMembersOfAlliance(Long allianceId) {
        if (allianceId == null) {
            throw new IllegalArgumentException("Alliance ID cant be null");
        }
        List<AllianceMember> members = new ArrayList<>();
        for (AllianceMemberEntity allianceMemberEntity : allianceMemberDao.getAllAllianceMembers(allianceId)) {
            members.add(mapper.map(allianceMemberEntity, AllianceMember.class));
        }
        return members;
    }

    @Override
    public List<Alliance> getAllAlliances() {

        List<Alliance> alliances = new ArrayList<>();
        for (AllianceEntity allianceEntity : allianceDao.getAllAlliances()) {
            alliances.add(mapper.map(allianceEntity, Alliance.class));
        }
        return alliances;
    }

    @Override
    public Alliance getAllianceByName(String name) {
        if (name == null) {
            throw new IllegalArgumentException("Name cant be null");
        }
        return mapper.map(allianceDao.getAllianceByName(name), Alliance.class);
    }

    @Override
    public void joinAlliance(Long playerId, Long allianceId) {
        if (playerId == null) {
            throw new IllegalArgumentException("Player ID cant be null");
        }
        if (allianceId == null) {
            throw new IllegalArgumentException("Alliance ID cant be null");
        }
        AllianceMember allianceMember = new AllianceMember();
        allianceMember.setAllianceId(allianceId);
        allianceMember.setPlayerId(playerId);
        allianceMemberDao.addAllianceMember(mapper.map(allianceMember, AllianceMemberEntity.class));
    }

    @Override
    public void leaveAlliance(Long playerId) {
        if (playerId == null) {
            throw new IllegalArgumentException("Player ID cant be null");
        }       
        Alliance alliance = this.getAllianceOfPlayer(playerId);
        allianceMemberDao.deleteAllianceMember(playerId);
        if(this.getMembersOfAlliance(alliance.getId()).isEmpty()){
            allianceDao.deleteAlliance(alliance.getId());
        }        
    }

    @Override
    public Alliance getAllianceOfPlayer(Long playerId) {
        if (playerId == null) {
            throw new IllegalArgumentException("Player ID cant be null");
        }
        return mapper.map(allianceDao.getAllianceOfPlayer(playerId), Alliance.class);
    }
}
